//
//  CZOriginalPictureView.swift
//  SinaBlog
//
//  Created by 李超 on 16/3/26.
//  Copyright © 2016年 lichao. All rights reserved.
//

import UIKit
import SDWebImage

class CZOriginalPictureView: UICollectionView {
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //流水布局
    let pictureLayout = UICollectionViewFlowLayout()
    
    
    //保存url的变量
    var pictureUrls:[NSURL]?{
        //需要重新跟新自身的约束以适应有无图片的情况
        didSet{
            
            let viewSize = setUpitemSizeAndViewSize()
            
                self.snp_updateConstraints(closure: { (make) -> Void in
                    
                    make.size.equalTo(viewSize)
                    
                })
            //刷新数据
            reloadData()
 
        }
        
        
    }
    //计算itemSize
    private let maxColum:Int = 3
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        
        super.init(frame: frame, collectionViewLayout: pictureLayout)
        //将数据源代理设置为自己
        dataSource = self
        //注册collctionViewCell
        registerClass(CZPictureCell.self, forCellWithReuseIdentifier: "pictureCell")
        backgroundColor = UIColor.whiteColor()
        //设置自身的大小以及item的大小
       // setUpitemSizeAndViewSize()
    
    }
    //设置item的大小以及自身的大小
    func setUpitemSizeAndViewSize()->CGSize{
        
         let itemSpacing:CGFloat = 10.0
         let picturViewItemHW:CGFloat = (UIScreen.mainScreen().bounds.width - 3 * margin - 2 * itemSpacing) / 3.0
         let pictureViewItemSize:CGSize = CGSizeMake(picturViewItemHW, picturViewItemHW)
        
        let count = pictureUrls?.count ?? 0
        if count == 0{
            return CGSizeZero
        }
        //只有一个则返回图片的原大小
        if count == 1 {
            
            var size = pictureViewItemSize
            let urlString = pictureUrls?.last?.absoluteString
            
            if let image = SDImageCache.sharedImageCache().imageFromDiskCacheForKey(urlString){
                
                 size = image.size
            }
            
            if size.width < 40{
                size.width = 40
            }
            if size.width > UIScreen.mainScreen().bounds.width*0.5{
                size.width = UIScreen.mainScreen().bounds.width*0.5
                
            }
            
            pictureLayout.itemSize = size
            pictureLayout.minimumLineSpacing = 0
            pictureLayout.minimumInteritemSpacing = 0
            return size
        }
        //其他情况下的itemsize都一样间距也一样
        pictureLayout.itemSize = pictureViewItemSize
        pictureLayout.minimumLineSpacing = itemSpacing
        pictureLayout.minimumInteritemSpacing = itemSpacing
        //当2张或者4张图时，分为2列其他情况分为3列
        
        var colum = maxColum
        
        if count == 2 || count == 4{
            colum = 2
        }
        
        
        let row = (colum-1 + count) / colum
        
        //根据行数列数来进行计算view的size
        let viewW = margin + CGFloat(colum) * picturViewItemHW + CGFloat(colum - 1) * itemSpacing
        let viewH = margin + CGFloat(row) * picturViewItemHW + CGFloat(row-1) * itemSpacing
        
        return CGSizeMake(viewW, viewH)
    }
    
   

}

//在拓展中实现数据源代理协议
extension CZOriginalPictureView:UICollectionViewDataSource{
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
       //有值则返回对应的个数，无值则返回0
       return pictureUrls?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
       let cell = collectionView.dequeueReusableCellWithReuseIdentifier("pictureCell", forIndexPath: indexPath) as! CZPictureCell
        cell.imageUrl = pictureUrls?[indexPath.item]
        return cell
        
    }
    
}